/**
 * Created by zhanglihe on 15/9/9.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'ckeditor',
    'validation',
    'calendar',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, ckeditor, Validation, calendar, datepicker) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function () {
            var _this = this;
            new Model({
                container: 'main-view',
                template: 'operation-tpl',
                tag_name: 'div',
                url: _global.url.api + 'operation_words',
                callback: function () {
                    loaded();
                }
            }).fetch();
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
        },

        event: function () {
            this.data = {};
            var _this = this;


            $("#btn-save").on("click", function (e) {
                e.preventDefault();
                _this.data.keywords = $("textarea[name='keywords']").val();
                _this.data.search_official = $('input[name="search_official"]').val();
                // _this.data.search_default_keyword= $.trim($("#search_default_keyword").val());
                _this.data.url = $('#url').val();
                _this.data.started_at = $('input[name="started_at"]').val();
                _this.data.ended_at = $('input[name="ended_at"]').val();
                if (_this.data.keywords == '') {
                    alert('请填写关键字列表');
                    return false;
                }
                if (_this.data.started_at == '') {
                    alert('请填写开始时间');
                    return false;
                }
                if (_this.data.ended_at == '') {
                    alert('请填写开始时间');
                    return false;
                }
                if (new Model({
                        url: _global.url.api + 'operation_words',
                        data: _this.data
                    }).post()) {
                    //_this.collection.reload();
                    window.location.reload();
                }


            });
        },
    };

    new index();

});